# -*- coding: utf-8 -*-
import os
import time
import subprocess
import psutil

PROCESS_NAME = "main.py"
LOG_FILE = "monitor.log"

def is_process_running():
    for proc in psutil.process_iter(['name', 'cmdline']):
        if proc.info['name'] == 'python' and PROCESS_NAME in ' '.join(proc.info['cmdline']):
            return True
    return False

def start_process():
    subprocess.Popen(["python3", PROCESS_NAME])

def mainscript():
    while True:
        if not is_process_running():
            with open(LOG_FILE, "a") as f:
                f.write(f"{time.ctime()} - 进程未运行，正在重启...\n")
            start_process()
        time.sleep(60)

if __name__ == "__main__":
    mainscript()